﻿using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.DynamicProxy;
using CNKI.TPI.Web.Search.IBLL;
using System.Linq.Expressions;
using System.Data;
namespace CNKI.TPI.Web.UI.Controllers.Admin
{
    public class Admin_SubmitController : BaseController
    {
        ISingleDBPublishService singleService = null;
        ISubmitService submitService = null;
        ISubmitAdminService submitAdminService = null;
        public Admin_SubmitController(ISingleDBPublishService singleService, ISubmitService submitService, ISubmitAdminService submitAdminService)
        {
            this.singleService = generator.CreateInterfaceProxyWithTarget<ISingleDBPublishService>(singleService, new IInterceptor[] { new LogInterceptor() });
            this.submitService = generator.CreateInterfaceProxyWithTarget<ISubmitService>(submitService, new IInterceptor[] { new LogInterceptor() });
            this.submitAdminService = generator.CreateInterfaceProxyWithTarget<ISubmitAdminService>(submitAdminService, new IInterceptor[] { new LogInterceptor() });

        }
        /// <summary>
        /// 在线提交设置
        /// </summary>
        /// <returns></returns>
        public ActionResult SubmitOption()
        {
            IList<SYS_CMS_DATABASE> list = singleService.GetDBList(new List<SearchType>() { SearchType.TPI});//获取数据库列表
            ViewBag.DBList = list;
            IList<SYS_CMS_OPTION> submitlist = submitService.GetSubmitDBList(1);//获取已提交数据库列表
            ViewBag.SubmitList = submitlist;
            return PartialView("SubmitOption");
        }
        /// <summary>
        ///在线提交页面配置 
        /// </summary>
        /// <returns></returns>
        public ActionResult SubmitPage()
        {
            return PartialView("SubmitPageList");
        }
        /// <summary>
        /// 数字对象配置
        /// </summary>
        /// <returns></returns>
        public ActionResult SubmitNumber()
        {
            return PartialView("SubmitNumber");
        }
        /// <summary>
        /// 不合格因素配置
        /// </summary>
        /// <returns></returns>
        public ActionResult SubmitReasons()
        { // 不合格因素
            ViewBag.ErrorReason = submitAdminService.GetErrorInfo();
            return PartialView("SubmitReasons");
        }

        public bool SaveOptionDB(IList<SYS_CMS_OPTION> list)
        {
            return submitAdminService.SaveOption(list);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        ///  
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SearchSubmitNumber(FormCollection form)
        {
            int pageIndexNow = 1;// 当前页    
            string name = "";
            if (!String.IsNullOrEmpty(Request["pageIndexNow"]))
            {
                pageIndexNow = int.Parse(Request["pageIndexNow"]);
            }
            if (!String.IsNullOrEmpty(Request["hidedbname"]))
            {
                name = Request["hidedbname"].ToString();
            }

            PageList(pageIndexNow, name);
            return PartialView("SubmitNumberResult");
        }
        private void PageList(int pageIndexNow, string name)
        {
            int pageSize = 10;//每页显示记录数
            int sumPage = 0;//一共多少页
            int disPgae = 8;  //默认显示页数，当页数大于该数值时，按规定格式显示
            int count = 0;//总条数

            IList<SYS_CMS_DATABASE> list = singleService.GetDBListPageSize(pageIndexNow, pageSize, "all", name, out count, new List<SearchType>() { SearchType.TPI });// all所有发布状态
            if (list != null && list.Count > 0)
            {
                ViewBag.DbList = list;
            }
            sumPage = count / pageSize; //一共多少页
            if (count % pageSize != 0)
            {
                sumPage = sumPage + 1;
            }
            ColumnAliasHelper com = new ColumnAliasHelper();
            ViewBag.TotalPage = sumPage;
            ViewBag.PageList = com.GetPageList(pageIndexNow, sumPage, disPgae);
            ViewBag.PageNow = pageIndexNow;//当前页
            ViewBag.PageSize = pageSize;
            ViewBag.Count = count;

        }

        public bool SaveErrorList(IList<SYS_CMS_ERRORINFOCONFIG> list)
        {
            return submitAdminService.SaveErrorList(list);
        }
        public JsonResult GetNumField(string DbCode)
        {
            IList<SYS_CMS_DATABASE_FIELD> list = submitAdminService.GetNumField(DbCode);
            //获取字段
            var res = new JsonResult();
            res.Data = list;
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;

        }
        public bool UpdateCmsDataBase(string papertype, string field, string timecheck, string sysid)
        {
            return submitAdminService.UpdateCmsDataBase(papertype, field, timecheck, sysid);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        ///  
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SearchSubmit(FormCollection form)
        {
            int pageIndexNow = 1;// 当前页    
            string type = "";
            string name = "";
            if (!String.IsNullOrEmpty(Request["pageIndexNow"]))
            {
                pageIndexNow = int.Parse(Request["pageIndexNow"]);
            }
            if (!String.IsNullOrEmpty(Request["type"]))
            {
                type = Request["type"].ToString();
            }
            if (!String.IsNullOrEmpty(Request["HiddenDbName"]))
            {
                name = Request["HiddenDbName"].ToString();
            }
            int pageSize = 10;//每页显示记录数
            int sumPage = 0;//一共多少页
            int disPgae = 8;  //默认显示页数，当页数大于该数值时，按规定格式显示
            int count = 0;//总条数

            IList<SYS_CMS_DATABASE> list = singleService.GetDBListPageSize(pageIndexNow, pageSize, "all", name, out count, new List<SearchType>() { SearchType.TPI });// all所有发布状态
            if (list != null && list.Count > 0)
            {
                foreach (SYS_CMS_DATABASE item in list)
                {
                    IList<SYS_CMS_SUBMIT> sublist = submitAdminService.IsEditStyle(item.DatabaseCode, "1");
                    if (sublist != null && sublist.Count > 0)
                    {
                        item.StyleFlag = 1;
                    }
                }
                ViewBag.DbList = list;
            }
            sumPage = count / pageSize; //一共多少页
            if (count % pageSize != 0)
            {
                sumPage = sumPage + 1;
            }
            ColumnAliasHelper com = new ColumnAliasHelper();
            ViewBag.TotalPage = sumPage;
            ViewBag.PageList = com.GetPageList(pageIndexNow, sumPage, disPgae);
            ViewBag.PageNow = pageIndexNow;//当前页
            ViewBag.PageSize = pageSize;
            ViewBag.Count = count;
            return PartialView("SubmitPageResult");
        }
        /// <summary>
        /// 提交页面编辑
        /// </summary>
        /// <param name="dbcode"></param>
        /// <returns></returns>
        public ActionResult SubmitEdit(string dbcode, int dbid)
        {
            ViewBag.DbCode = dbcode;
            SYS_CMS_DATABASE objdb = singleService.GetDatabaseByID(dbid);
            if (objdb != null)
            {
                ViewBag.DbName = objdb.DatabaseName;

            }
            string username = "";
            if (SessionHelper.GetUserInfo() != null)
            {
                username = SessionHelper.GetUserInfo().UserName;
            }
            ViewBag.UserName = username;

            IList<SYS_CMS_DATABASE_FIELD> listField = singleService.GetDBField(dbcode); //数据库字段
            ViewBag.ListField = listField;

            IList<SYS_CMS_SUBMIT> submitlist = submitAdminService.IsEditStyle(dbcode, "1");//获取已设置数据库字段
            ViewBag.SubmitList = submitlist;
            if (submitlist != null && listField != null)
            {
                foreach (SYS_CMS_SUBMIT obj in submitlist)
                {
                   if (listField.Where(c => c.Field == obj.FIELDNAME) != null && listField.Where(c => c.Field == obj.FIELDNAME).Count() > 0)
                    {
                        SYS_CMS_DATABASE_FIELD objField = listField.Where(c => c.Field == obj.FIELDNAME).ToList()[0];
                        obj.IsRequired = objField.FieldValueUnique; //CMC 非空字段
                        obj.IsBlank = objField.NullEnable;
                    }
                }
            }

            IList<SubmitFieldType> list = submitAdminService.GetSelectStyle();//控件列表
            ViewBag.SelectIList = list;
            string optionselect = "";
            foreach (SubmitFieldType item in list)
            {
                optionselect += "<option value=" + (int)item + ">" + StringHelper.GetSubmitFieldTypeDesc(item) + "</option>";
            }
            ViewBag.SubmitSelect = optionselect; //下拉列表 html串
            ViewBag.SelectFlag = (int)SubmitFieldType.ComboBox; //下拉列表标识，2

            IList<SYS_CMS_DATABASE> listdb = singleService.GetDBList();//获取数据库列表
            ViewBag.DBList = listdb;

            return PartialView("SubmitPageEdit");
        }
        /// <summary>
        /// 保存设置的提交样式
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool StyleSave(IList<SYS_CMS_SUBMIT> list)
        {
            if (list != null && list.Count > 0)
            {
                return submitAdminService.SaveStyle(list);
            }
            else
            {
                return false;
            }
        }
        public JsonResult GetDbField(string DbCode)
        {
            List<string> columnsField = new List<string>();
            if (DbCode == "SYS_CMS_USER")
            {
                columnsField = submitAdminService.GetUserColumnName();
            }
            else
            {
                IList<SYS_CMS_DATABASE_FIELD> list = new List<SYS_CMS_DATABASE_FIELD>();
                list = singleService.GetDBField(DbCode);
                columnsField = list.Select(x => x.Field).ToList();
            }
            //获取字段
            var res = new JsonResult();
            res.Data = columnsField;
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;

        }
        /// <summary>
        /// 删除设置的提交样式
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool StyleDel(string dbcode)
        {
            return submitAdminService.DelStyle(dbcode, "1");

        }

    }
}
